package com.xycake.controller.admin;

import com.xycake.entity.User;
import com.xycake.service.AdminService;
import com.xycake.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/reg")
    public Map<String, Object> reg(User user) {
        Map<String, Object> map = new HashMap<>();
        map.put("msg", adminService.regUser(user));
        return map;
    }

    @PostMapping("/login")
    public Map<String, Object> login(User user, HttpSession httpSession) {
        Map<String, Object> map = new HashMap<>();
        User u = adminService.userLogin(user);
        if (u == null) {
            map.put("msg", "用户名或密码错误！");
        } else {
            httpSession.setAttribute("loginUser",u);
            map.put("msg", "success");
            map.put("type",u.getType());
        }
        return map;
    }

    @RequestMapping("/logout")
    public Map<String, Object> logout(HttpSession httpSession) {
        httpSession.invalidate();
        Map<String, Object> map = new HashMap<>();
        return map;
    }
}
